using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class AddressDAL
    {
        public static int Save(AddressInfo info)
        {
            string sql = string.Format("update [address] set IsDefault=0 where [AdminID]=@AdminID and [UserID]=@UserID;insert into [address]([AdminID],[UserID],[Province],[City],[Area],[RealName],[Mobile],[Address],[IsDefault],[CreateDate]) values(@AdminID,@UserID,@Province,@City,@Area,@RealName,@Mobile,@Address,@IsDefault,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@Province", info.Province),
                new SqlParameter("@City", info.City),
                new SqlParameter("@Area", info.Area),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@Address", info.Address),
                new SqlParameter("@IsDefault", info.IsDefault),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(AddressInfo info)
        {
            string sql = string.Format("update [address] set [AdminID]=@AdminID,[Province]=@Province,[City]=@City,[Area]=@Area,[RealName]=@RealName,[Mobile]=@Mobile,[Address]=@Address,[IsDefault]=@IsDefault where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Province", info.Province),
                new SqlParameter("@City", info.City),
                new SqlParameter("@Area", info.Area),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@Address", info.Address),
                new SqlParameter("@IsDefault", info.IsDefault),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static AddressInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[address]");
            return SimpleData(dr);
        }
        public static List<AddressInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[address]");
            return ListSimpleData(dt);
        }
        public static List<AddressInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [address].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [address].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[address]", out count);
            return ListSimpleData(dt);
        }
        public static List<AddressInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [address]"));
            return ListSimpleData(dt);
        }
        public static bool IsExist(int id, int userid, string realname,string mobile,string address)
        {
            string sql = string.Format("select ID from [address] where RealName='{0}' and Mobile='{1}' and Address='{2}' and ID<>{3} and UserID={4}", realname, mobile, address, id, userid);
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql)) > 0 ? true : false;
        }
        public static bool Set_IsDefault(int adminid, int userid, int id)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [address] set IsDefault=0 where AdminID={0} and UserID={1};update [address] set IsDefault=1 where AdminID={0} and ID ={2};", adminid, userid, id));
        }
        public static List<AddressInfo> FindByUserID(int adminid, int userid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [address] where AdminID={0} and UserID={1} order by ID desc", adminid, userid));
            return ListSimpleData(dt);
        }
        public static AddressInfo FindByDefault(int adminid, int userid)
        {
            DataRow dr = CommonDAL.GetDataRow(string.Format("select top 1 * from [address] where AdminID={0} and UserID={1} and IsDefault=1 order by ID desc", adminid, userid));
            return SimpleData(dr);
        }
        private static AddressInfo SimpleData(DataRow dr)
        {
            AddressInfo info = new AddressInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Province = DataConvert.ToString(dr["Province"]);
                info.City = DataConvert.ToString(dr["City"]);
                info.Area = DataConvert.ToString(dr["Area"]);
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.Address = DataConvert.ToString(dr["Address"]);
                info.IsDefault = DataConvert.ToInt(dr["IsDefault"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static AddressInfo AllData(DataRow dr)
        {
            AddressInfo info = new AddressInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Province = DataConvert.ToString(dr["Province"]);
                info.City = DataConvert.ToString(dr["City"]);
                info.Area = DataConvert.ToString(dr["Area"]);
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.Address = DataConvert.ToString(dr["Address"]);
                info.IsDefault = DataConvert.ToInt(dr["IsDefault"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static List<AddressInfo> ListSimpleData(DataTable dt)
        {
            List<AddressInfo> list = new List<AddressInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<AddressInfo> ListAllData(DataTable dt)
        {
            List<AddressInfo> list = new List<AddressInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

